package com.jingyanzi.sys.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingyanzi.sys.dao.SysAddressMapper;
import com.jingyanzi.sys.domain.AddressDO;
import com.jingyanzi.sys.domain.query.AddressQueryDO;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.IAddressService;
import com.jingyanzi.sys.entity.SysAddress;
import com.jingyanzi.sys.entity.SysAddressExample;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("addressService")
public class AddressServiceImpl extends BaseServiceImpl implements IAddressService {

	@Autowired
	private SysAddressMapper pAddressMapper;

	@Override
	public ResultDO add(AddressDO address) {

		ResultDO result = new ResultSupport(true);
		if (address == null || address.getName() == null || address.getCode() == null) {
			result.setSuccess(false);
			result.setErrorMsg("添加失败:不能为空");
			return result;
		}
		Date date = new Date();
		SysAddress pAddress = new SysAddress();
		BeanUtilsExtends.copyProperties(pAddress, address);
		pAddress.setModifitime(date);
		pAddress.setCreatetime(date);
		pAddress.setStatus(1);
		pAddress.setIsDelete(1);
		int insert = -1;
		try {
			SysAddressExample example = new SysAddressExample();
			example.createCriteria().andIsDeleteEqualTo(1).andCodeEqualTo(pAddress.getCode());
			List<SysAddress> list = pAddressMapper.selectByExample(example);
			if (list != null && list.size() != 0) {
				result.setSuccess(false);
				result.setErrorMsg("添加失败code存在");
				return result;
			}
			insert = pAddressMapper.insert(pAddress);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg("出异常");
		}
		if (insert < 0) {
			result.setSuccess(false);
			result.setErrorMsg("添加失败");
		} else {
			result.setModel(ResultDO.FIRST_MODEL_KEY, pAddress.getId());
		}
		return result;
	}

	@Override
	public ResultDO list(AddressQueryDO address) {

		ResultDO result = new ResultSupport(true);

		if (address.getPageNO() != 0 && address.getPageRows() != 0) {
			PageHelper.startPage(address.getPageNO(), address.getPageRows());
		}
		SysAddressExample example = new SysAddressExample();

		if (address.getNameOrCode() != null && !"".equals(address.getNameOrCode())) {

			example.createCriteria().andNameLike("%" + address.getNameOrCode() + "%");
			example.or().andCodeLike("%" + address.getNameOrCode() + "%");
		}
		example.createCriteria().andIsDeleteEqualTo(1);

		List<AddressDO> addressDOs = null;
		try {

			List<SysAddress> list = pAddressMapper.selectByExample(example);

			if (address.isPage()) {
				PageInfo<SysAddress> pageInfo = new PageInfo<SysAddress>(list);
				list = pageInfo.getList();
				long count = pageInfo.getTotal();
				result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			}
			addressDOs = getAddressDOList(list);
			result.setModel(ResultDO.FIRST_MODEL_KEY, addressDOs);
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
			result.setErrorMsg("异常");
		}
		return result;
	}
	
	@Override
	public ResultDO getBycode(String code) {
		ResultDO result = new ResultSupport(true);
		if (code == null) {
			result.setSuccess(false);
			result.setErrorMsg("数据异常");
			return result;
		}
		AddressDO p = new AddressDO();
		SysAddressExample example = new SysAddressExample();
		try {
			example.createCriteria().andCodeEqualTo(code);
			List<SysAddress> list = pAddressMapper.selectByExample(example);
			SysAddress address = list.get(0);
			BeanUtilsExtends.copyProperties(p, address);
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
			result.setErrorMsg("后台异常");
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, p);
		result.setSuccess(true);
		return result;
	}
	

	@Override
	public ResultDO get(Integer id) {
		ResultDO result = new ResultSupport(true);
		if (id == null || id == 0) {
			result.setSuccess(false);
			result.setErrorMsg("数据异常");
			return result;
		}
		AddressDO p = new AddressDO();
		SysAddressExample example = new SysAddressExample();
		try {
			example.createCriteria().andIdEqualTo(id);
			List<SysAddress> list = pAddressMapper.selectByExample(example);
			SysAddress address = list.get(0);
			BeanUtilsExtends.copyProperties(p, address);
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
			result.setErrorMsg("后台异常");
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, p);
		result.setSuccess(true);
		return result;
	}

	@Override
	public ResultDO modify(AddressDO address) {
		ResultDO result = new ResultSupport(true);
		if (address == null || address.getId() == null || address.getId() == 0) {
			result.setSuccess(false);
			result.setErrorMsg("数据异常");
			return result;
		}
		SysAddress p = new SysAddress();
		BeanUtilsExtends.copyProperties(p, address);
		int i = -1;
		try {
			i = pAddressMapper.updateByPrimaryKeySelective(p);
		} catch (Exception e) {
			// TODO: handle exception
			result.setSuccess(false);
			result.setErrorMsg("后台异常");
		}
		if (i < 0) {
			result.setSuccess(false);
			result.setErrorMsg("修改失败");
		} else {
			result.setSuccess(true);
		}
		return result;
	}

}
